<?php
//执行商品信息的增、删
//导入配置文件核函数库文件
//error_reporting(0);
require("dbconfig.php");
require("functions.php");
require("./class/sqlHelper.php");
$db = new Opmysqli(HOST, USER, PASSWORD, DBNAME);
?>
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>浏览所有商品</title>   
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{
    font-family:微软雅黑;
    font-size:16px;
    color:red;
    }
#color_c
{
    color:black;
}
.div_c
{
    margin-top:30px;
}
</style>
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
    <div class="well div_c">
<?php
if($resError = $db->msg_error())
{
    echo $resError;
    exit(0);
}

//获取action参数的值，做对应的操作
switch($_GET["action"])
{
    case "add": //添加
        //1、获取添加信息
        $goodsId = $_POST["goodsId"];
        $goodsName = $_POST["goodsName"];
        $price = $_POST["price"];
        //165M
        $MQuantity = $_POST["MQuantity"];
        $MLongth = $_POST["MLongth"];
        $MWidth = $_POST["MWidth"];
        //170L
        $LQuantity = $_POST["LQuantity"];
        $LLongth = $_POST["LLongth"];
        $LWidth = $_POST["LWidth"];
        //175XL
        $XLQuantity = $_POST["XLQuantity"];
        $XLLongth = $_POST["XLLongth"];
        $XLWidth = $_POST["XLWidth"];
        //180(XXL)
        $XXLQuantity = $_POST["XXLQuantity"];
        $XXLLongth = $_POST["XXLLongth"];
        $XXLWidth = $_POST["XXLWidth"];
        //185(XXXL)
        $XXXLQuantity = $_POST["XXXLQuantity"];
        $XXXLLongth = $_POST["XXXLLongth"];
        $XXXLWidth = $_POST["XXXLWidth"];
        //商品描述
        $desc = $_POST["desc"];
        //码
        $s165 = $_POST["s165"];
        $s170 = $_POST["s170"];
        $s175 = $_POST["s175"];
        $s180 = $_POST["s180"];
        $s185 = $_POST["s185"];

        $total = $MQuantity + $LQuantity + $XLQuantity + $XXLQuantity + $XXXLQuantity;
        $arr1 = array(
            "商品ID" => array($goodsId),
            "165M" => array($MQuantity, $MLongth, $MWidth),
            "170L" => array($LQuantity, $LLongth,  $LWidth),
            "175XL" => array($XLQuantity, $XLLongth, $XLWidth),
            "180XXL" => array($XXLQuantity, $XXXLLongth, $XXXLWidth),
            "185XXXL" => array($XXXLQuantity, $XXXLLongth, $XXXLWidth)
                    );
        $arr2 = array("商品名称" => $goodsName, "商品描述" => $desc);

          //2、验证
       $res = checkValidate($arr1, $arr2, $price);
       if ($res["error"])
       {
            //3、执行图片上传
            $upinfo = uploadFile("pic", "./uploads");
            if ($upinfo["error"] === false)
            {
                die("图片信息上传失败！".$upinfo["info"]);
            }
            else
            {
                $pic = $upinfo["info"];   //获取上传的图片名
                $curtime = date('Y-n-j G:i:s');
                //4、执行图片缩放
                imageUpdateSize("./uploads/".$pic, 100, 100, $pre = "s_");
                //5、sql语句，并执行添加
            $sql = "insert into clothes(id, name, price, total, pic, note, addtime) ".
                "values({$goodsId}, '{$goodsName}', {$price}, {$total}, '{$pic}'".
                ", '{$desc}', '{$curtime}');";

            $sql .= "insert into clothes_parameter(id, longth, width".
                 ", sizeCode, quantity) ".
                "values({$goodsId}, {$MLongth}, {$MWidth}, '{$s165}', {$MQuantity});";

            $sql .= "insert into clothes_parameter(id, longth, width".
                 ", sizeCode, quantity) ".
                 "values({$goodsId}, {$LLongth}, {$LWidth}, '{$s170}', {$LQuantity});";
                
            $sql .= "insert into clothes_parameter(id, longth, width".
                ", sizeCode, quantity) ".
                "values({$goodsId}, {$XLLongth}, {$XLWidth}, '{$s175}', {$XLQuantity});";

            $sql .= "insert into clothes_parameter(id, longth, width".
                ", sizeCode, quantity) ".
                "values({$goodsId}, {$XXLLongth}, {$XXLWidth}, '{$s180}', {$XXLQuantity});";

            $sql .= "insert into clothes_parameter(id, longth, width".
                ", sizeCode, quantity) ".
            "values({$goodsId}, {$XXXLLongth}, {$XXXLWidth}, '{$s185}', {$XXXLQuantity});";

            $insertRes = $db->mysqli_multi_dml($sql);
                if($insertRes == 1)
                {
                    echo "<span id=\"color_c\">发布衣服商品成功！</span>";
                }
                else
                {
                    echo "发布衣服商品失败！".$insertRes;
                    exit();
                }
            }
       }
       else
       {
            echo $res["info"];
       }
        //6、输出结果
        break;
    case "del": //删除
        $sql = "delete from clothes where id={$_GET['id']}"; 
        $res_de = $db->sql_dml($sql);
        if ($res_de["error"])
        {
            //删除图片
            @unlink("./uploads/".$_GET['picname']);
            @unlink("./uploads/s_".$_GET['picname']);
            echo "<span id=\"color_c\">";
            echo $res_de["info"];
            echo "</span>"; 
        }
        else
        {
            echo $res_de["info"];
            exit(0);
        }
        break;
}
//关闭数据库

$db->close_conn();

?>
</div>
</div>
<script src="./bootstrap/js/bootstrap.min.js"></script>
<script src="./js/jquery-1.9.1.min.js"></script>
 </body>
 </html>
